﻿<%
Class Class_Page

	'翻页模块
	Private iPagesize	'每页记录数
	Private	iPageCount  '总页数
	Private	iPageCurr	'当前面
	Private sPageName	'页数参数名
	Private sField		'字段名
	Private sTable		'表名
	Private sCondition	'条件
	Private sOrderBy	'排序
	Private sPkey		'主键
	Private sPostString	'查询串

	Private Sub Class_Initialize()
		'类初始化

		'BadStrs 为注入关键字的过滤
		BadStrs ="'|;|--|and"
		iPageSize=20
		sPageName="Page"
		iOrder=0
		sField=" * "
		sTable=""
		sCondition=" 1=1 "
		sOrderBy=""
		sPkey=""
		sPostString=""

	End Sub

	Public Function Get_Data(byval ValueName)
		Get_Data = Trim(ReplaceBad(Trim(Request(ValueName)),BadStrs))
	End Function

	Public Function ReplaceBad(str,BadStr)    
		Dim VarStr,Strs,T_String,i
		T_String = str
		Strs = Split(BadStr,"|")
		For i=0 To Ubound(Strs)
			T_String=Replace(T_String,Strs(i),"")
		Next
		ReplaceBad = T_String
	End Function


	'#########################################################################
	'翻页模块开始
	'#########################################################################

	'================================================================
	' PageSize 设置每一页记录条数,默认10记录
	'================================================================
	Public Property Let PageSize(ByVal intPageSize)
	iPageSize=CheckNum(intPageSize,0,0,iPageSize,0) 
	End Property


	'================================================================
	' Field 取得字段名
	'================================================================
	Public Property Let Field(ByVal strField)
		sField=IIf(Len(strField)<1,sField,strField)
	End Property

	'================================================================
	' Table 取得表名
	'================================================================
	Public Property Let Table(ByVal strTable)
	sTable=strTable
	End Property

	'================================================================
	' Condition 取得条件
	'================================================================
	Public Property Let Condition(ByVal strCondition)
		sCondition=strCondition
	End Property

	'================================================================
	' OrderBy 取得排序字段，可以多个字段排序。
	'================================================================
	Public Property Let OrderBy(ByVal strOrderBy)
	sOrderBy=strOrderBy
	End Property

	'================================================================
	' Pkey 取得主键
	'================================================================
	Public Property Let Pkey(ByVal strPkey)
	sPkey=strPkey
	End Property

	'================================================================
	' PostString 取得查询串 以|分隔
	'================================================================
	Public Property Let PostString(ByVal strPostString)
	sPostString=strPostString
	End Property

	'================================================================
	' GetRecCount 取得记录总数
	'================================================================
	Private Function GetRecCount()
		Dim iRecCount 
		'writebr "SELECT COUNT("&sPkey&") FROM "&sTable&" WHERE "&sCondition
		iRecCount=Conn.Execute("SELECT COUNT("&sPkey&") FROM "&sTable&" WHERE "&sCondition,0,1)(0)
		GetRecCount=iRecCount
	End Function


	'================================================================
	' RecCount 修正记录总数
	'================================================================
	Public Property Get RecCount()
		RecCount=GetRecCount()
	End Property

	Public Property Get PageCount()
		s=0
		'记录总数
		i=RecCount()
		'当前页
		If i>0 Then			
			s = Abs(Int(-Abs(i/iPageSize)))'页数
		End If
		PageCount = s

	End Property

	'================================================================
	' ResultSet 返回分页后的记录集
	'================================================================
	Public Property Get ResultSet()
	s=Null
	'记录总数
	i=RecCount()
	'当前页
	If i>0 Then			
		iPageCount=Abs(Int(-Abs(i/iPageSize)))'页数
		iPageCurr=CheckNum(Request.Form(sPageName),1,1,1,iPageCount)'当前页

		Set rsTemp=server.CreateObject("Adodb.RecordSet")
		Set Cm=Server.CreateObject("Adodb.Command")
		Cm.CommandType=4
		Cm.ActiveConnection=Conn

		Cm.CommandText="usp_GetPageData"
		
		Cm.parameters(1) = sTable '@tblName varchar(255), -- 表名

		Cm.parameters(2) = sPkey

		Cm.parameters(3) = iPageCurr '@PageIndex int = 1, -- 页码

		Cm.parameters(4) = iPageSize '@PageSize int = 10, -- 页尺寸

		Cm.parameters(5) = sField '@strGetFields varchar(1000) = '*', -- 需要返回的列 

		Cm.parameters(6) = sOrderBy '@fldName varchar(255)='', -- 排序的字段名

		Cm.parameters(7) = sCondition '@strWhere varchar(1500) = '' -- 查询条件 (注意: 不要加 where)

		Set rsTemp = cm.execute()
	


		'writebr iPageSize
		If Not rsTemp.Eof Then 
			s = rsTemp.GetRows(iPageSize)
		Else
			s = ""
		End If
		rsTemp.close
		Set rsTemp=Nothing
	End If
	ResultSet=s
	End Property


	'================================================================
	' 输入:检查字符,是否有最小值,是否有最大值,最小值(默认数字),最大值
	'================================================================
	Private Function CheckNum(ByVal strStr,ByVal blnMin,ByVal blnMax,ByVal intMin,ByVal intMax)
	Dim i,s,iMi,iMa
	s=Left(Trim(""&strStr),32):iMi=intMin:iMa=intMax
	If IsNumeric(s) Then
		i=CDbl(s)
		i=IIf(blnMin=1 And i<iMi,iMi,i)
		i=IIf(blnMax=1 And i>iMa,iMa,i)
	Else
		i=iMi
	End If
	CheckNum=i
	End Function

	'================================================================
	' 输入:简化条件判断
	'================================================================
	Private Function IIf(ByVal blnBool,ByVal strStr1,ByVal strStr2)
	Dim s
	If blnBool Then
		s=strStr1
	Else
		s=strStr2
	End If
	IIf=s
	End Function


	'================================================================
	' 分页方法
	'================================================================
	Public Sub ShowPage_Simple()

		Dim Array_PostString,sQueryString
		sQueryString = ""

		If RecCount>0   Then

			Array_PostString = split(sPostString,"|")
			For i=0 To ubound(Array_PostString)
				sQueryString = sQueryString &"<INPUT TYPE='hidden' name="&Array_PostString(i)&" value='"&Get_Data(Array_PostString(i))&"'>"&Chr(13)&Chr(10)
			Next
			Response.Write "<Script Language=""JavaScript"" type=""text/JavaScript"">"

			Response.Write "function GoPage(){"
			Response.Write "	var page=document.pagefrm.page.value;"
			Response.Write "	var max_Page="&iPageCount&";"
			Response.Write "	if (page>0 && page<=max_Page){"
			Response.Write "		document.pagefrm.submit();"
			Response.Write "		}"
			Response.Write "	else{"
			Response.Write "		alert(""页码不在合法范围内！"");"
			Response.Write "		return false;"
			Response.Write "	}"
			Response.Write "}"
			Response.Write "function Page(page){"
			Response.Write "	document.pagefrm.page.value=page;"
			Response.Write "	GoPage();"
			Response.Write "}"
			Response.Write "</Script>"

			Response.Write "<form action="""" method=""Post"" name=""pagefrm"" onsubmit=""return GoPage();"">"
			Response.Write "  共"&RecCount()&"条信息, 当前显示第 "&iPageSize*(iPageCurr-1)+1&" - "

			If iPageSize*iPageCurr>RecCount() THen
				Response.write RecCount()
			Else
				Response.write iPageSize*iPageCurr
			End If
			Response.Write "   条,  共"&iPageCount&"页"

			If iPageCurr>6 Then
				xi = iPageCurr-5
				yi = iPageCurr+4
				 Response.write "<A HREF='javascript:Page("&xi-1&")'>上一页</A> "
				For i= xi  to yi
					If i> iPageCount Then Exit For 
					If i=iPageCurr Then
						Response.write " "&i&" "
					Else
						Response.write " <A HREF='javascript:Page("&i&")'>"&i&"</A> "
					End If
					
				 Next
				If iPageCount>i Then Response.write "...&nbsp;<A HREF='javascript:Page("&i&")'>下一页</A>&nbsp;"
			Else
				For i= 1  to 10 
					If i> iPageCount Then Exit For 
					If i=iPageCurr Then
						Response.write i
					Else
						Response.write " <A HREF='javascript:Page("&i&")'>"&i&"</A> "
					End If
					
				Next
				If iPageCount>i Then Response.write "...&nbsp;<A HREF='javascript:Page("&i&")'>下一页</A>&nbsp;"

			End If
					
			Response.Write "	直接到第 "
			Response.Write "<INPUT TYPE=""text"" NAME=""page"" size=5>&nbsp;页" 
			Response.Write "<INPUT type=image src=""/images/button_small2_qw.gif"" align=absMiddle value=""确 定""  Name=submit >"
			Response.Write sQueryString
			Response.Write "</form>"
		End If
	End Sub

	'================================================================
	' 分页方法2 简单分页
	'================================================================
	Public Sub ShowPage()
		Dim Array_PostString,sQueryString
		sQueryString = ""

		If RecCount>0   Then

			Array_PostString = split(sPostString,"|")
			For i=0 To ubound(Array_PostString)
				sQueryString = sQueryString &"<INPUT TYPE='hidden' name="&Array_PostString(i)&" value='"&Get_Data(Array_PostString(i))&"'>"&Chr(13)&Chr(10)
			Next
			Response.Write "<Script Language=""JavaScript"" type=""text/JavaScript"">"

			Response.Write "function GoPage(){"
			Response.Write "	var page=document.pagefrm.page.value;"
			Response.Write "	var max_Page="&iPageCount&";"
			Response.Write "	if (page>0 && page<=max_Page){"
			Response.Write "		document.pagefrm.submit();"
			Response.Write "		}"
			Response.Write "	else{"
			Response.Write "		alert(""页码不在合法范围内！"");"
			Response.Write "		return false;"
			Response.Write "	}"
			Response.Write "}"
			Response.Write "function Page(page){"
			Response.Write "	document.pagefrm.page.value=page;"
			Response.Write "	GoPage();"
			Response.Write "}"
			Response.Write "</Script>"

			Response.Write "<table><form action="""" method=""Post"" name=""pagefrm"" onsubmit=""return GoPage();""><tr><td>"
				
				Response.Write "共"&RecCount&"条记录&nbsp;&nbsp;"
				Response.Write "第"&iPageCurr&"页/共"&iPageCount&"页&nbsp;&nbsp;"

				

				If iPageCurr>1  Then 
					Response.write "<A HREF='javascript:Page(1)'>首页</A>&nbsp;&nbsp;"
					Response.write "<A HREF='javascript:Page("&iPageCurr-1&")'>上一页</A>&nbsp;&nbsp;"
				Else

					Response.write "首页&nbsp;&nbsp;"
					Response.write "上一页&nbsp;&nbsp;"
				End If

				If iPageCurr< iPageCount Then 
					Response.write "<A HREF='javascript:Page("&iPageCurr+1&")'>下一页</A>&nbsp;&nbsp;"
					Response.write "<A HREF='javascript:Page("&iPageCount&")'>尾页</A> "
				Else
					Response.write "下一页&nbsp;&nbsp;"
					Response.write "尾页&nbsp;&nbsp;"
					
				End If
				
				Response.write "转到第&nbsp;&nbsp;"
				
				Response.write "<select name=""pageTo"" onChange=""Page(this.value)"" id=""pageTo"">"
				For i = 1 To iPageCount
					Response.write "<option value="""&i&""" "&GetSelected(iPageCurr,i)&">"& i &"</option>" &Chr(13)&Chr(10)
				Next
				Response.write "</select>"

			Response.Write "<INPUT TYPE=""hidden"" NAME=""page"" size=5>"
			Response.Write sQueryString
			
			Response.Write "</td></tr></form></table>"
		End If
	End Sub

	'================================================================
	' 分页方法3 样式表分页
	'================================================================
	Public Sub ShowPage2()
		Dim Array_PostString,sQueryString
		sQueryString = ""

		If RecCount>0   Then

			Array_PostString = split(sPostString,"|")
			For i=0 To ubound(Array_PostString)
				sQueryString = sQueryString &"<INPUT TYPE='hidden' name="&Array_PostString(i)&" value='"&Get_Data(Array_PostString(i))&"'>"&Chr(13)&Chr(10)
			Next
			Response.Write "<Script Language=""JavaScript"" type=""text/JavaScript"">"

			Response.Write "function GoPage(){"
			Response.Write "	var page=document.pagefrm.page.value;"
			Response.Write "	var max_Page="&iPageCount&";"
			Response.Write "	if (page>0 && page<=max_Page){"
			Response.Write "		document.pagefrm.submit();"
			Response.Write "		}"
			Response.Write "	else{"
			Response.Write "		alert(""页码不在合法范围内！"");"
			Response.Write "		return false;"
			Response.Write "	}"
			Response.Write "}"
			Response.Write "function Page(page){"
			Response.Write "	document.pagefrm.page.value=page;"
			Response.Write "	GoPage();"
			Response.Write "}"
			Response.Write "</Script>"

			Response.Write "<form action="""" method=""Post"" name=""pagefrm"" onsubmit=""return GoPage();"">"
				

			Response.Write "<INPUT TYPE=""hidden"" NAME=""page"" size=5>"

			
			Response.write "<span class=""textinfo f14blue"">共"&RecCount&"条信息每页"&iPageSize&"条</span>"

			If iPageCurr>6 Then
				xi = iPageCurr-5
				yi = iPageCurr+4
				Response.write "<input type=""button"" class=""page f14blue"" value=""&lt;&lt;""value=""&lt;&lt;"" onclick=""Page("&(iPageCurr-1)&")"&"""/>"
				For i= xi  to yi
					If i> iPageCount Then Exit For 
					If i=iPageCurr Then
						Response.write "<input type=""button"" class=""curren f14bwhite"" value="""&i&""" />"
					Else
						Response.write "<input type=""button"" class=""page f14blue"" value="""&i&""" onclick=""Page("&i&")"&"""/>"
					End If
					
				 Next
				'If iPageCount>i Then

				If iPageCount>i Then Response.write "<input type=""button"" class=""page f14blue"" value=""&gt;&gt;""value=""&gt;&gt;"" onclick=""Page("&i+1&")"&"""/>"

				
				
			Else
				For i= 1  to 10 
					If i> iPageCount Then Exit For 
					If i=iPageCurr Then
						Response.write "<input type=""button"" class=""curren f14bwhite"" value="""&i&""" />"
					Else
						Response.write "<input type=""button"" class=""page f14blue"" value="""&i&""" onclick=""Page("&i&")"&"""/>"
					End If
					
				Next

				If iPageCount>i Then Response.write "<input type=""button"" class=""page f14blue"" value=""&gt;&gt;"" onclick=""Page("&iPageCurr+1&")"&"""/>"

			End If
			Response.write sQueryString
			Response.Write "</form>"

		End If
	End Sub

	'================================================================
	' 分页方法4 样式表分页 酒店使用
	'================================================================
	Public Sub ShowPage3()
		Dim Array_PostString,sQueryString
		sQueryString = ""

		If RecCount>0   Then

			Array_PostString = split(sPostString,"|")
			For i=0 To ubound(Array_PostString)
				sQueryString = sQueryString &"<INPUT TYPE='hidden' name="&Array_PostString(i)&" value='"&Get_Data(Array_PostString(i))&"'>"&Chr(13)&Chr(10)
			Next
			Response.Write "<Script Language=""JavaScript"" type=""text/JavaScript"">"

			Response.Write "function GoPage(){"
			Response.Write "	var page=document.pagefrm.page.value;"
			Response.Write "	var max_Page="&iPageCount&";"
			Response.Write "	if (page>0 && page<=max_Page){"
			Response.Write "		document.pagefrm.submit();"
			Response.Write "		}"
			Response.Write "	else{"
			Response.Write "		alert(""页码不在合法范围内！"");"
			Response.Write "		return false;"
			Response.Write "	}"
			Response.Write "}"
			Response.Write "function Page(page){"
			Response.Write "	document.pagefrm.page.value=page;"
			Response.Write "	GoPage();"
			Response.Write "}"
			Response.Write "</Script>"

			Response.Write "<form action="""" method=""Post"" name=""pagefrm"" onsubmit=""return GoPage();"">"
				

			Response.Write "<INPUT TYPE=""hidden"" NAME=""page"" size=>"


			If iPageCurr>1 Then
				Response.Write "<span class=""disabled""><a href=""#"" onclick=""Page("&iPageCurr-1&")"&""">&laquo;上页</a></span>"
			Else
				'Response.write "<a href=""#"" onclick=""return false;""><  Prev</a> "
				Response.Write "<span class=""disabled"">&laquo;上页</span>"
			End If 

			Response.Write "<span class=""pages_a"">"

			If iPageCurr>6 Then
				xi = iPageCurr-5
				yi = iPageCurr+4

				If yi>iPageCount Then 
				xi = iPageCount-9
				yi = iPageCount
				End If

				If xi<=0 Then  xi=1


				For i= xi  to yi
					If i> iPageCount Then Exit For 
					If i=iPageCurr Then
						'Response.write "<span class=""current"">"&i&"</span>"
						Response.Write "<a class=""current"">"&i&"</a>"
					Else
						Response.write "<a href=""#"" onclick=""Page("&i&")"&""">"&i&"</a>"
						'Response.write "<a href=""#?page=2"">2</a>"
					End If
					
				 Next

			Else

				For i= 1  to 10 
					If i> iPageCount Then Exit For 
					If i=iPageCurr Then
						Response.Write "<a class=""current"">"&i&"</a>"
					Else
						Response.write "<a href=""#"" onclick=""Page("&i&")"&""">"&i&"</a>"
					End If
					
				Next


			End If
			Response.Write "</span>"

			If iPageCurr<iPageCount Then 
				'Response.write "<a href=""#"" onclick=""Page("&iPageCurr+1&")"&""">Next  > </a> "
				Response.write "<span class=""disabled""><a href=""#"" onclick=""Page("&iPageCurr+1&")"">下页&raquo;</a></span>"
			Else
				'Response.write "<a href=""#"" onclick=""return false;"">Next  ></a> "
				Response.Write "<span class=""disabled""><a href=""#"" onclick=""return false;"">下页&raquo;</a></span>"
			End If 
			Response.Write "<span class=""disabled"">共"&iPageCount&"页</span>"


			Response.write sQueryString

			Response.Write "</form>"

		End If
	End Sub
	
	Public Sub ShowPage4()
		Dim Array_PostString,sQueryString
		sQueryString = ""

		If RecCount>0   Then

			Array_PostString = split(sPostString,"|")
			For i=0 To ubound(Array_PostString)
				sQueryString = sQueryString &"<INPUT TYPE='hidden' name="&Array_PostString(i)&" value='"&Get_Data(Array_PostString(i))&"'>"&Chr(13)&Chr(10)
			Next
			Response.Write "<Script Language=""JavaScript"" type=""text/JavaScript"">"

			Response.Write "function GoPage(){"
			Response.Write "	var page=document.pagefrm.page.value;"
			Response.Write "	var max_Page="&iPageCount&";"
			Response.Write "	if (page>0 && page<=max_Page){"
			Response.Write "		document.pagefrm.submit();"
			Response.Write "		}"
			Response.Write "	else{"
			Response.Write "		alert(""页码不在合法范围内！"");"
			Response.Write "		return false;"
			Response.Write "	}"
			Response.Write "}"
			Response.Write "function Page(page){"
			Response.Write "	document.pagefrm.page.value=page;"
			Response.Write "	GoPage();"
			Response.Write "}"
			Response.Write "</Script>"

			Response.Write "<form action="""" method=""Post"" name=""pagefrm"" onsubmit=""return GoPage();"">"
				

			Response.Write "<INPUT TYPE=""hidden"" NAME=""page"" size=>"

			Response.Write "<em>"&iPageCurr&"/"&iPageCount&"</em>"

			If iPageCurr>1 Then
				'Response.Write "<span class=""disabled""><a href=""#"" onclick=""Page("&iPageCurr-1&")"&""">&laquo;上页</a></span>"
				Response.Write "<a href=""#"" onclick=""Page("&iPageCurr-1&")"&""">« Pre</a>"
			Else
				'Response.Write "<span class=""disabled"">&laquo;上页</span>"
				Response.Write "<a href=""#"">« Pre</a>"
			End If 

			'Response.Write "<span class=""pages_a"">"

			If iPageCurr>6 Then
				xi = iPageCurr-5
				yi = iPageCurr+4

				If yi>iPageCount Then 
				xi = iPageCount-9
				yi = iPageCount
				End If

				If xi<=0 Then  xi=1


				For i= xi  to yi
					If i> iPageCount Then Exit For 
					If i=iPageCurr Then
						'Response.Write "<a class=""current"">"&i&"</a>"
						Response.Write "<span>"&i&"</span>"
					Else
						Response.write "<a href=""#"" onclick=""Page("&i&")"&""">"&i&"</a>"
					End If
					
				 Next

			Else

				For i= 1  to 10 
					If i> iPageCount Then Exit For 
					If i=iPageCurr Then
						'Response.Write "<a class=""current"">"&i&"</a>"
						Response.Write "<span>"&i&"</span>"
					Else
						Response.write "<a href=""#"" onclick=""Page("&i&")"&""">"&i&"</a>"
					End If
					
				Next


			End If
			'Response.Write "</span>"

			If iPageCurr<iPageCount Then 
				'Response.write "<a href=""#"" onclick=""Page("&iPageCurr+1&")"&""">Next  > </a> "
				'Response.write "<span class=""disabled""><a href=""#"" onclick=""Page("&iPageCurr+1&")"">下页&raquo;</a></span>"
				Response.write "<a href=""#"" onclick=""Page("&iPageCurr+1&")"">Next »</a>"
			Else
				'Response.write "<a href=""#"" onclick=""return false;"">Next  ></a> "
				'Response.Write "<span class=""disabled""><a href=""#"" onclick=""return false;"">下页&raquo;</a></span>"
				Response.Write "<a href=""#"" onclick=""return false;"">Next »</a>"
			End If 
			'Response.Write "<span class=""disabled"">共"&iPageCount&"页</span>"


			Response.write sQueryString

			Response.Write "</form>"

		End If
	End Sub

	'#########################################################################
	'上传模块结束
	'#########################################################################

	Private Sub Class_Terminate() 
		Set Conn = Nothing 
	End Sub

END Class
%>
